package gunfire.events;

import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractImageEvent;
import com.megacrit.cardcrawl.helpers.CardLibrary;
import com.megacrit.cardcrawl.localization.EventStrings;
import com.megacrit.cardcrawl.vfx.cardManip.ShowCardAndObtainEffect;
import gunfire.GunfireMod;

import static gunfire.GunfireMod.makeEventPath;

public class RedChestEvent2 extends AbstractImageEvent {
    public static final String ID = GunfireMod.makeID("RedChestEvent2");
    private static final EventStrings eventStrings = CardCrawlGame.languagePack.getEventString(ID);
    private static final String NAME = eventStrings.NAME;
    private static final String[] DESCRIPTIONS = eventStrings.DESCRIPTIONS;
    private static final String[] OPTIONS = eventStrings.OPTIONS;
    public static final String IMG = makeEventPath("RedChest.png");
    private int screenNum; //0初始 1roll
    private final int PRICE = 10;
    private AbstractCard card;
    private CardGroup cardList;
    private int cardIndex;

    public RedChestEvent2() {
        super(NAME, DESCRIPTIONS[0], IMG);
        if(AbstractDungeon.player.gold >= PRICE){
            this.imageEventText.setDialogOption(OPTIONS[0]);
        } else {
            this.imageEventText.setDialogOption(OPTIONS[1], true);
        }
        this.imageEventText.setDialogOption(OPTIONS[2]);

        this.screenNum = 0;
        this.cardList = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
        this.cardIndex = -1;
        this.cardList.group.addAll(AbstractDungeon.commonCardPool.group);
        this.cardList.group.addAll(AbstractDungeon.uncommonCardPool.group);
        this.cardList.group.addAll(AbstractDungeon.rareCardPool.group);
        this.cardList.shuffle();
    }

    @Override
    protected void buttonEffect(int i) {
        switch (this.screenNum){
            case 0:
                switch (i){
                    case 0:
                        AbstractDungeon.player.loseGold(PRICE);
                        this.roll();
                        break;
                    case 1:
                        this.openMap();
                        this.leave();
                        break;
                }
                break;
            case 1:
                switch (i){
                    case 0:
                        AbstractDungeon.effectList.add(new ShowCardAndObtainEffect(this.card, (float) Settings.WIDTH / 2.0F, (float)Settings.HEIGHT / 2.0F));
                        this.leave();
                        break;
                    case 1:
                        AbstractDungeon.player.loseGold(PRICE);
                        this.roll();
                        break;
                    case 2:
                        this.openMap();
                        this.leave();
                        break;
                }
                break;
            case 2:
                this.openMap();
                break;
        }
    }

    private void roll(){
        this.screenNum = 1;
        this.cardIndex = (this.cardIndex + 1) % this.cardList.group.size();
        this.imageEventText.clearAllDialogs();
        this.card = this.cardList.group.get(this.cardIndex).makeCopy();
        this.imageEventText.setDialogOption(OPTIONS[3] + this.getColorfulCardName(this.card) + "。", CardLibrary.getCopy(this.card.cardID));
        if(AbstractDungeon.player.gold >= PRICE){
            this.imageEventText.setDialogOption(OPTIONS[4]);
        } else {
            this.imageEventText.setDialogOption(OPTIONS[5], true);
        }
        this.imageEventText.setDialogOption(OPTIONS[6]);
    }

    private void leave(){
        this.screenNum = 2;
        this.imageEventText.clearAllDialogs();
        this.imageEventText.setDialogOption(OPTIONS[6]);
    }

    private String getColorfulCardName(AbstractCard c){
        if(c.rarity == AbstractCard.CardRarity.UNCOMMON){
            return " #b" + c.name + " ";
        } else if(c.rarity == AbstractCard.CardRarity.RARE){
            return " #y" + c.name + " ";
        }
        return c.name;
    }
}
